<template>
  <div class="passenger">
    <a-card title="每日车次">
      <a-form layout="inline" :model="formObj">
        <a-form-item>
          <a-date-picker
            v-model:value="formObj.date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
          />
        </a-form-item>
        <a-form-item>
          <TrainSelect v-model:code="formObj.code" width="200px" />
        </a-form-item>
        <a-form-item>
          <a-select
            v-model:value="formObj.type"
            style="width: 200px"
            allow-clear
            placeholder="请选择车次类型"
          >
            <a-select-option
              v-for="(item, index) in getEnumList('train_type').value"
              :key="index"
              :value="item.code"
            >
              <span>{{ item.description }}</span>
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="getList">查询</a-button>
        </a-form-item>
        <a-form-item>
          <a-button type="primary" disabled @click="add">新增</a-button>
        </a-form-item>
        <a-form-item>
          <a-button @click="reset">重置</a-button>
        </a-form-item>
        <a-form-item>
          <a-button danger @click="handlerGenDaily">手动生成车次信息</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-table :data-source="dataList" :columns="columns" :pagination="false">
      <template #action="{ record }">
        <a-button type="link" size="small" @click="edit(record)">编辑</a-button>
        &nbsp;&nbsp;&nbsp;
        <a-popconfirm title="确认删除吗?" @confirm="deleteFn(record)">
          <a-button type="link" danger size="small">删除</a-button>
        </a-popconfirm>
        &nbsp;&nbsp;&nbsp;
        <a-popconfirm
          title="生成座位会删除已有座位信息，确认生成吗?"
          @confirm="generatorSeats(record)"
        >
          <a-button type="link" size="small">生成座位</a-button>
        </a-popconfirm>
      </template>
    </a-table>
    <a-pagination
      v-model:current="formObj.page"
      v-model:page-size="formObj.pageSize"
      align="right"
      :total="formObj.total"
      show-size-changer
      style="margin-top: 20px"
      @change="onChange"
    />
    <a-modal
      v-model:open="dialog"
      :title="formState.id ? '编辑' : '新增'"
      width="700px"
      ok-text="确认"
      cancel-text="取消"
      center
      @ok="handleOk"
      @cancel="cancel"
    >
      <a-form :model="formState">
        <a-form-item label="日期">
          <a-date-picker
            v-model:value="formState.date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
          />
        </a-form-item>
        <a-form-item label="车次编号">
          <TrainSelect v-model:code="formObj.code" />
        </a-form-item>
        <a-form-item label="车次类型">
          <a-select v-model:value="formState.type">
            <a-select-option
              v-for="(item, index) in getEnumList('train_type').value"
              :key="index"
              :value="item.code"
            >
              <span>{{ item.description }}</span>
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="始发站">
          <StationSelect v-model:name="formState.start" />
        </a-form-item>
        <a-form-item label="始发站拼音">
          <a-input v-model:value="formState.startPinyin" placeholder="请输入" disabled />
        </a-form-item>
        <a-form-item label="出发时间">
          <a-time-picker
            v-model:value="formState.startTime"
            placeholder="Select Time"
            value-format="HH:mm:ss"
            format="HH:mm:ss"
          />
        </a-form-item>
        <a-form-item label="终点站">
          <StationSelect v-model:name="formState.end" />
        </a-form-item>
        <a-form-item label="终点站拼音">
          <a-input v-model:value="formState.endPinyin" placeholder="请输入" disabled />
        </a-form-item>
        <a-form-item label="到站时间">
          <a-time-picker
            v-model:value="formState.endTime"
            placeholder="Select Time"
            value-format="HH:mm:ss"
            format="HH:mm:ss"
          />
        </a-form-item>
      </a-form>
    </a-modal>
    <a-modal
      v-model:open="dialog2"
      title="生成车次"
      width="500px"
      ok-text="确认"
      cancel-text="取消"
      center
      :confirm-loading="confirmLoading"
      @ok="handleOk2"
      @cancel="cancel2"
    >
      <a-form :model="formState2">
        <a-form-item label="日期">
          <a-date-picker
            v-model:value="formState2.date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
          />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>
<script setup lang="ts">
import { nextTick, onMounted, ref, watch } from 'vue';
import { DailyTrainApi } from '../../api/dailyTrain';
import { message } from 'ant-design-vue';
import { pinyin } from 'pinyin-pro';
import StationSelect from '@/components/Station-Select.vue';
import { getEnumList } from '@/utils/getEnum';
import TrainSelect from '@/components/Train-Select.vue';
import dayjs from 'dayjs';
import confirm from 'ant-design-vue/es/modal/confirm';
onMounted(() => {
  console.log('111', getEnumList('train_type'));
});
const formState = ref<any>({
  id: '',
  code: '',
  date: null,
  type: null,
  start: '',
  startPinyin: '',
  startTime: '',
  end: '',
  endPinyin: '',
  endTime: '',
});
const dialog = ref(false);

const columns = ref([
  {
    title: '日期',
    dataIndex: 'date',
    key: 'date',
  },
  {
    title: '车次编号',
    dataIndex: 'code',
    key: 'code',
  },
  {
    title: '车次类型',
    dataIndex: 'typeName',
    key: 'typeName',
  },
  {
    title: '始发站',
    dataIndex: 'start',
    key: 'start',
  },
  {
    title: '始发站拼音',
    dataIndex: 'startPinyin',
    key: 'startPinyin',
  },
  {
    title: '出发时间',
    dataIndex: 'startTime',
    key: 'startTime',
  },
  {
    title: '终点站',
    dataIndex: 'end',
    key: 'end',
  },
  {
    title: '终点站拼音',
    dataIndex: 'endPinyin',
    key: 'endPinyin',
  },
  {
    title: '到站时间',
    dataIndex: 'endTime',
    key: 'endTime',
  },
  {
    title: '操作',
    align: 'center',
    dataIndex: 'action',
    fixed: 'right',
    width: 240,
    slots: { customRender: 'action' }, // 使用 slots 而不是 render 函数
  },
]);
const formObj = ref({
  date: null,
  code: '',
  type: null,
  page: 1,
  pageSize: 10,
  total: 0,
});

onMounted(() => {
  getList();
});

watch(
  () => formState.value.start,
  () => {
    if (formState.value.start) {
      formState.value.startPinyin = pinyin(formState.value.start, {
        toneType: 'none',
      }).replaceAll(' ', '');
    } else {
      formState.value.startPinyin = '';
    }
  },
  { immediate: true }
);

watch(
  () => formState.value.end,
  () => {
    if (formState.value.end) {
      formState.value.endPinyin = pinyin(formState.value.end, {
        toneType: 'none',
      }).replaceAll(' ', '');
    } else {
      formState.value.endPinyin = '';
    }
  },
  { immediate: true }
);

const dataList = ref([]);

const getList = async (): Promise<void> => {
  const { data } = await DailyTrainApi.query(formObj.value);
  dataList.value = data.records || [];
  formObj.value.total = data.total;
};
const reset = (): void => {
  formObj.value.code = '';
  formObj.value.type = null;
  formObj.value.date = null;
  getList();
};
const onChange = (current: number, pageSize: number): void => {
  console.log(current, pageSize);
  formObj.value.page = current;
  formObj.value.pageSize = pageSize;
  getList();
};
const add = (): void => {
  console.log('add');
  dialog.value = true;
};
const edit = async (record: any): Promise<void> => {
  console.log('record', record);
  const { data } = await DailyTrainApi.detail(record.id);

  formState.value = data;
  nextTick(() => {
    dialog.value = true;
  });
};
const deleteFn = async (record: any): Promise<void> => {
  await DailyTrainApi.delete(record.id);
  message.success('删除成功');
  getList();
};
const generatorSeats = async (record: any): Promise<void> => {
  await DailyTrainApi.generatorSeats(record.code);
  message.success('生成成功');
};
const handleOk = async (): Promise<void> => {
  if (formState.value.id) {
    await DailyTrainApi.update(formState.value);
  } else {
    await DailyTrainApi.add(formState.value);
  }
  message.success('保存成功');
  dialog.value = false;
  getList();
};
const cancel = (): void => {
  Object.keys(formState.value).forEach(key => {
    formState.value[key] = '';
  });
  console.log('cancel');
};

const dialog2 = ref(false);
const confirmLoading = ref(false);
const formState2 = ref({
  date: null,
});
const handlerGenDaily = () => {
  dialog2.value = true;
};
const handleOk2 = async () => {
  confirmLoading.value = true;
  let date = dayjs(formState2.value.date).format('YYYY-MM-DD');
  try {
    await DailyTrainApi.genDailyTrain(date);
    message.success('生成成功');
    getList();
  } catch (error) {
    console.log('error', error);
  } finally {
    dialog2.value = false;
    confirmLoading.value = false;
  }
};
const cancel2 = () => {
  dialog2.value = false;
  formState2.value.date = null;
};
</script>

<style scoped lang="less">
.passenger {
  :global(.ant-modal .ant-modal-footer) {
    text-align: center;
  }
}
</style>
